#include<bits/stdc++.h>
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define mid ((l+r)>>1)
#define File(xxx) freopen(xxx".in","r",stdin),freopen(xxx".out","w",stdout)
using namespace std;
namespace IO{
    char buff[1<<21],*p1=buff,*p2=buff;
    char getch(){
        return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;
    }
    template<typename T>
    void read(T &x){
        char ch=getch();int fl=1;x=0;
        while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}
        while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}
        x*=fl;
    }
    template<typename T,typename ...Args>
    void read(T &x,Args& ...args){
        read(x);read(args...);
    }
    char obuf[1<<21],*p3=obuf;
    void putch(char ch){
        if(p3-obuf<(1<<21))*p3++=ch;
        else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
    }
    void pc(const char *s){
    	while(*s)putch(*s),s++;
    }
    char ch[100];
    template<typename T>
    void write(T x){
        if(!x)return putch('0');
        if(x<0)putch('-'),x*=-1;
        int top=0;
        while(x)ch[++top]=x%10+48,x/=10;
        while(top)putch(ch[top]),top--;
    }
    template<typename T,typename ...Args>
    void write(T x,Args ...args){
        write(x),putch(' '),write(args...);
    }
    void flush(){fwrite(obuf,p3-obuf,1,stdout);p3=obuf;}
}
using namespace IO;
const int N=5e5+5,V=5e5;
int n,maxx=0,minx=V,maxy=0,miny=V;
struct DSU{
	int fa[N],siz[N];
	int tot;pair<int,int>st[N];
	void Init(int len){
		tot=0;
		for(int i=1;i<=len;i++)fa[i]=i,siz[i]=1;
		return;
	}
	inline int find(int x){return fa[x]==x?x:find(fa[x]);}
	inline void merge(int x,int y){
		x=find(x),y=find(y);
		if(x^y){
			if(siz[x]>siz[y])swap(x,y);
			fa[x]=y;
			siz[y]+=siz[x];
			st[++tot]={x,y}; 
		}
	}
	inline void Re(int last){
		int x,y;
		while(tot>last){
			x=st[tot].first,y=st[tot].second;
			tot--;
			fa[x]=x;
			siz[y]-=siz[x];
		}
		return;
	}
}S1,S2;
map<pair<int,int>,int>id;
bool vis[N];
vector<int>w1[N],W1[N],w2[N],W2[N];
void Init(){
	for(int i=minx;i<=maxx;i++){
		sort(w1[i].begin(),w1[i].end());
		int last=-2;
		for(auto x:w1[i]){
			if(vis[x]){
				if(last!=x-1)
					W1[i].push_back(x);
				last=x;
			}
		}
		for(auto x:w1[i-1])
			vis[x]=0;
		for(auto x:w1[i])
			vis[x]=1;
	}memset(vis,0,sizeof vis);
	
	for(int i=miny;i<=maxy;i++){
		sort(w2[i].begin(),w2[i].end());
		int last=-2;
		for(auto x:w2[i]){
			if(vis[x]){
				if(last!=x-1)
					W2[i].push_back(x);
				last=x;
			}
		}
		for(auto x:w2[i-1])
			vis[x]=0;
		for(auto x:w2[i])
			vis[x]=1;
	}memset(vis,0,sizeof vis);
}
ll ans;
int t[N];
int Siz[N];
vector<int>to[N];
void dfs(int x,int fa){
	for(auto y:to[x]){
		if(y==fa)continue;
		dfs(y,x);
		Siz[x]+=Siz[y];
	}
	ans+=1ll*Siz[x]*(n-Siz[x]);
}
void sol1(){
	S1.Init(n),S2.Init(n);
	for(int i=maxx;i>=minx;i--){
		for(int j=1;j<(int)w1[i].size();j++)
			if(w1[i][j-1]==w1[i][j]-1)
				S2.merge(id[{i,w1[i][j-1]}],id[{i,w1[i][j]}]);
		for(auto x:W1[i+1])
			S2.merge(id[{i,x}],id[{i+1,x}]);
		t[i]=S2.tot;
	}
	for(int i=minx;i<maxx;i++){
		for(int j=1;j<(int)w1[i].size();j++)
			if(w1[i][j-1]==w1[i][j]-1)
				S1.merge(id[{i,w1[i][j-1]}],id[{i,w1[i][j]}]);
		S2.Re(t[i+1]);
		
		int rt=-1;
		for(auto x:W1[i+1]){
			int a=S1.find(id[{i,x}]),b=S2.find(id[{i+1,x}]);
			to[a].clear(),to[b].clear();
			Siz[a]=S1.siz[a],
			Siz[b]=S2.siz[b];
			if(rt==-1)rt=a;
		}
		for(auto x:W1[i+1]){
			int a=S1.find(id[{i,x}]),b=S2.find(id[{i+1,x}]);
			to[a].push_back(b);
			to[b].push_back(a);
		}
		dfs(rt,0);
		
		for(auto x:W1[i+1])
			S1.merge(id[{i,x}],id[{i+1,x}]);
	}
}
void sol2(){
	S1.Init(n),S2.Init(n);
	for(int i=maxy;i>=miny;i--){
		for(int j=1;j<(int)w2[i].size();j++)
			if(w2[i][j-1]==w2[i][j]-1)
				S2.merge(id[{w2[i][j-1],i}],id[{w2[i][j],i}]);
		for(auto x:W2[i+1])
			S2.merge(id[{x,i}],id[{x,i+1}]);
		t[i]=S2.tot;
	}
	for(int i=miny;i<maxy;i++){
		for(int j=1;j<(int)w2[i].size();j++)
			if(w2[i][j-1]==w2[i][j]-1)
				S1.merge(id[{w2[i][j-1],i}],id[{w2[i][j],i}]);
		S2.Re(t[i+1]);
		
		int rt=-1;
		for(auto x:W2[i+1]){
			int a=S1.find(id[{x,i}]),b=S2.find(id[{x,i+1}]);
			to[a].clear(),to[b].clear();
			Siz[a]=S1.siz[a],
			Siz[b]=S2.siz[b];
			if(rt==-1)rt=a;
		}
		for(auto x:W2[i+1]){
			int a=S1.find(id[{x,i}]),b=S2.find(id[{x,i+1}]);
			to[a].push_back(b);
			to[b].push_back(a);
		}
		dfs(rt,0);
		
		for(auto x:W2[i+1])
			S1.merge(id[{x,i}],id[{x,i+1}]);
	}
}
signed main(){
	File("game");
	read(n);
	for(int i=1;i<=n;i++){
		int x,y;
		read(x,y);
		id[{x,y}]=i;
		w1[x].push_back(y);
		w2[y].push_back(x);
		maxx=max(maxx,x);
		minx=min(minx,x);
		maxy=max(maxy,y);
		miny=min(miny,y);
	}
	Init();
	sol1();
	sol2();
	write(ans);
	return flush(),0;
}

